import App from "./App";
import uviewPlus from "uview-plus";
import util from "@/utils/util";
import request from "@/utils/request";
import { createPinia } from "pinia";
import myToast from "@/components/myToast/myToast";

const store = createPinia();
// #ifndef VUE3
import Vue from "vue";
Vue.config.productionTip = false;
App.mpType = "app";

try {
  function isPromise(obj) {
    return (
      !!obj &&
      (typeof obj === "object" || typeof obj === "function") &&
      typeof obj.then === "function"
    );
  }

  // 统一 vue2 API Promise 化返回格式与 vue3 保持一致
  uni.addInterceptor({
    returnValue(res) {
      if (!isPromise(res)) {
        return res;
      }
      return new Promise((resolve, reject) => {
        res.then((res) => {
          if (res[0]) {
            reject(res[0]);
          } else {
            resolve(res[1]);
          }
        });
      });
    },
  });
} catch (error) {}

const app = new Vue({
  ...App,
});
app.$mount();
// #endif

// #ifdef VUE3
import { createSSRApp } from "vue";
export function createApp() {
  const app = createSSRApp(App);
  app.component("myToast", myToast);
  app.use(uviewPlus);
  app.use(store);
  uni.$u.config.unit = "rpx";
  app.config.globalProperties.$util = util;
  app.config.globalProperties.$request = request;
  return {
    app,
  };
}
// #endif
